package cn.stevenyang.tests;

import cn.stevenyang.exceptions.VariableNotFoundException;
import cn.stevenyang.exceptions.VariablePathTooDeepException;
import cn.stevenyang.services.CallStackService;
import cn.stevenyang.services.MemoryService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CallStackServiceTest {
    @Test
    public void test() throws VariablePathTooDeepException, VariableNotFoundException {
        CallStackService callStackService = new CallStackService(new MemoryService());
        callStackService.setVariable("test", 1);
        callStackService.setVariable("test2.test1", 2);
        Assertions.assertEquals(1, callStackService.getVariableOrNull("test"));
        Assertions.assertEquals(2, callStackService.getVariableOrNull("test2.test1"));
        callStackService.pushNewStack();
        callStackService.setVariable("test3", 3);
        Assertions.assertEquals(3, callStackService.getVariableOrNull("test3"));
        callStackService.setVariable("test2.test1", 1);
        Assertions.assertEquals(1, callStackService.getVariableOrNull("test2.test1"));
        callStackService.pushNewStack();
        callStackService.setVariable("i", 100);
        Assertions.assertEquals(100, callStackService.getVariableOrNull("i"));
        callStackService.setVariable("i", 99);
        Assertions.assertEquals(99, callStackService.getVariableOrNull("i"));
        callStackService.pushNewStack();
        callStackService.setVariable("j", 50);
        callStackService.setVariable("i", 30);
        Assertions.assertEquals(50, callStackService.getVariableOrNull("j"));
        Assertions.assertEquals(30, callStackService.getVariableOrNull("i"));
        callStackService.popCallStack();
        callStackService.setVariable("i", 25);
        Assertions.assertEquals(25, callStackService.getVariableOrNull("i"));
        Assertions.assertNull(callStackService.getVariableOrNull("j"));
        Assertions.assertEquals(true, callStackService.hasDefinedVariable("i"));
        Assertions.assertEquals(false, callStackService.hasDefinedVariable("j"));
    }
}
